/*
*LoadingDialog.java
*Created on 2014-9-24 ����10:18 by Ivan
*Copyright(c)2014 Guangzhou Onion Information Technology Co., Ltd.
*http://www.cniao5.com
*/
package com.oscar.ehome.utils.view;

import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.oscar.ehome.R;


public class LoadingDialog {

    private Activity mContext;
    private Fragment c;

    private View mDialogView;
    private TextView mTxtMsg;

    private Dialog mDialog;


    public LoadingDialog(Activity context) {
        this.mContext = context;
        mDialogView = LayoutInflater.from(context)
                .inflate(R.layout.dialog_downloading, null);
        mTxtMsg = (TextView) mDialogView.findViewById(R.id.txtMsg);
        initDialog();
    }

    private void initDialog() {
        mDialog = new Dialog(mContext, R.style.dialog);
        mDialog.setCancelable(true);
        mDialog.setContentView(mDialogView);
        mDialog.setCanceledOnTouchOutside(false);
    }

    public void setMessage(CharSequence msg) {
        mTxtMsg.setText(msg);
    }

    public void setMessage(int msg) {
        mTxtMsg.setText(msg);
    }


    public void show(boolean canceledOnTouchOutside) {

        if (mContext == null) {
            return;
        }
        //
        if (mContext.isFinishing()) {
            System.out.println("context isfinishing.....");
            return;
        }

        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mContext == null) {
            return;
        }
        //
        if (mContext.isFinishing()) {
            System.out.println("context isfinishing.....");
            return;
        }

        if (mDialog != null && mDialog.isShowing() && !mContext.isFinishing()) {
            mDialog.dismiss();
        }
    }


    public boolean isShowing() {
        return mDialog.isShowing();
    }


    public interface OnLoadingDismissLisnter<T> {
        void onLoadingDismiss(T t);
    }
}
